package com.yidu.drug.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yidu.drug.common.Page;
import com.yidu.drug.common.Tools;
import com.yidu.drug.model.QtSumModel;
import com.yidu.drug.model.QualityTestingModel;
import com.yidu.drug.service.QtSumModelService;
import com.yidu.drug.service.QualityTestingModelService;
import com.yidu.drug.service.impl.QualityTestingModelServicelmpl;

@Controller
@RequestMapping("/qualityTestingModel")
public class QualityTestingModelAction {
	@Resource
	private QualityTestingModelService qualityTestingModelService;
	@Resource
	private QtSumModelService qtSumModelService;
	
	/**
	 * 查询所有数据
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("/findAllQualityTestingModel")
	@ResponseBody
	public Map<String, Object> findAllQualityTestingModel(HttpServletRequest request,HttpServletResponse response,String sumId,String qtPerson,String page,String limit,String dateOne,String dateTwo) throws UnsupportedEncodingException{
		System.out.println("进来了=-=-=-=--=");
		//UTF-8编码
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");


		System.out.println(sumId+"---------------------");

		System.out.println(qtPerson+"---------------------");
		//new出分页
		Page pages = new Page();
		//给分页赋值
		pages.setPage(Integer.valueOf(page));
		//给总页数赋值
		pages.setRows(Integer.valueOf(limit));

		//new出质检表
		QualityTestingModel qualityTestingModel = new QualityTestingModel();
		//给分页赋值
		qualityTestingModel.setPages(pages);
		//给sumId赋值
		qualityTestingModel.setSumId(sumId);
		//给qtPerson赋值
		qualityTestingModel.setQtPerson(qtPerson);
		//给DateOne赋值
		qualityTestingModel.setDateOne(dateOne);
		//给DateTwo赋值
		qualityTestingModel.setDateTwo(dateTwo);
		
		//写质检的集合，使用查询所有管理员数据方法
		List<QualityTestingModel> list = qualityTestingModelService.findAllQualityTestingModel(qualityTestingModel);
		//list集合for循环
		for(QualityTestingModel QualityTestingModel:list){
			System.out.println(QualityTestingModel.getQtPerson()+"========================");
			//new一个日期转换方法
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			//定义一个变量开始日期转换
			String str = sdf.format(QualityTestingModel.getQtTime());
			//把变量放入字段中
			QualityTestingModel.setQtTimes(str);
		}
		System.out.println("到了这一步=-=-=-=-=-=-=-");
		//定义一个long型的变量使用质检的分页方法
		Long count = qualityTestingModelService.findRows(qualityTestingModel);
		//map集合
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", count);
		map.put("data", list);
		System.out.println("多撒us户的撒肯定撒");
		//返回map集合
		return map;
		
	}
	
	/**
	 * 增加管理人员数据
	 * @return 
	 * @return
	 */
	@RequestMapping("/addQualityTestingModel")
	public int add(QualityTestingModel qualityTestingModel,HttpSession session) throws IOException{
		//返回一个增加管理人员数据方法
		return qualityTestingModelService.insertSelective(qualityTestingModel);
	}
	
	/**
	 * 计算
	 * @throws IOException 
	 */
	@RequestMapping("/update")
	@ResponseBody
	public String update(HttpSession session,QualityTestingModel qualityTestingModel) throws IOException{
		System.out.println(qualityTestingModel.getQtId()+"  "+qualityTestingModel.getNopassNum()+"  "+qualityTestingModel.getQtSumnum());
		//返回一个计算的方法
		return qualityTestingModelService.update(qualityTestingModel);
	}
	
	/**
	 * 计算
	 * @throws IOException 
	 */
	@RequestMapping("/updateTwo")
	@ResponseBody
	public String updateTwo(HttpSession session,String sumId,Integer sumNopassNum,Integer sumQtSumnum,String poId) throws IOException{
		//定义一个double型 变量 进行（总数量-未通过数量）/总数量的计算
		double d = (Double.valueOf(sumQtSumnum) - Double.valueOf(sumNopassNum)) / sumQtSumnum;
		//new出类型转换
		java.math.BigDecimal bg = new java.math.BigDecimal(d);
		//进行四舍五入
		java.math.BigDecimal bgs = bg.setScale(2,BigDecimal.ROUND_HALF_UP);
		//new出质检总报表
		QtSumModel qtSumModel = new QtSumModel();
		//给质检ID赋值
		qtSumModel.setSumId(sumId);
		//给总通过率赋值
		qtSumModel.setSumRate(bgs);
		//给未通过数量赋值
		qtSumModel.setSumNopass(sumNopassNum);
		//给备注赋值
		qtSumModel.setSumRemark("已质检");
		//定义一个变量得到质检总报表的修改方法(带上一个采购订单ID)
		int rows = qtSumModelService.updateByPrimaryKeySelective(qtSumModel,poId);
		//if循环如果大于0
		if(rows > 0){
			//返回ok
			return "ok";
		}else {
			//返回no
			return "no";
		}
	}
	
	@RequestMapping("/findAll")
	@ResponseBody
	public Map<String,Object> findAll(QualityTestingModel qualityTestingModel){
		//返回质检表的显示所有方法
		return qualityTestingModelService.findAll(qualityTestingModel);
	}
}
